package com.gzyj.jtt808.v2019.protocol.yuebiao;

import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.additionalinfo.JTT808_AddtionalInfo;

import io.netty.buffer.ByteBuf;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lhj
 */
@Data
@ApiModel("轮胎状态监测")
public class JTT808_0x0200_0x66 extends JTT808_AddtionalInfo implements Serializable {

    @ApiModelProperty("报警ID")
    public Long AlarmId;
    @ApiModelProperty("标志状态")
    public Byte FlagState;

    @ApiModelProperty("车速")
    public Byte Speed;
    @ApiModelProperty("高程")
    public Integer Altitude;

    @ApiModelProperty("纬度")
    public Long Latitude;

    @ApiModelProperty("经度")
    public Long Longitude;
    /// <summary>
    ///
    /// YYMMDDhhmmss
    /// BCD[6]
    /// </summary>
    @ApiModelProperty("日期时间")
    public Date AlarmTime;

    @ApiModelProperty("车辆状态")
    public Integer VehicleState;

    @ApiModelProperty("报警标识号")
    public  AlarmIdentificationProperty AlarmIdentification;

    /// <summary>
    /// 报警/事件信息列表
    /// </summary>
    public List<AlarmOrEventProperty> AlarmOrEvents = new ArrayList<>();


    public JTT808_0x0200_0x66() {
        super(0x66);
    }

    @Override
    public void encode(ByteBuf buf) {

        buf.writeByte(this.getID());
        buf.writeByte(65 + this.AlarmOrEvents.size() * 10);

        buf.writeInt(this.AlarmId.intValue());
        buf.writeByte(this.FlagState);
        buf.writeByte(this.Speed);
        buf.writeShort(this.Altitude);
        buf.writeInt(this.Latitude.intValue());
        buf.writeInt(this.Longitude.intValue());
        buf.writeBytes(HexBytesUtil.WriteDateTime_yyMMddHHmmss(this.AlarmTime));
        buf.writeShort(this.VehicleState);
        this.AlarmIdentification.encode(buf);
        buf.writeByte(this.AlarmOrEvents.size());
        for (AlarmOrEventProperty eventProperty : this.AlarmOrEvents) {
            eventProperty.encode(buf);
        }


    }

    @Override
    public JTT808_0x0200_0x66 decode(ByteBuf buf) {
        this.setLength(buf.readByte());
        this.AlarmId = buf.readUnsignedInt();
        this.FlagState = buf.readByte();
        this.Speed = buf.readByte();
        this.Altitude = buf.readUnsignedShort();
        this.Latitude = buf.readUnsignedInt();
        this.Longitude = buf.readUnsignedInt();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            this.AlarmTime = sdf.parse(HexBytesUtil.ReadDateTime_yyMMddHHmmss(HexBytesUtil.readByOffset(buf, 6)));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.VehicleState = buf.readUnsignedShort();

        this.AlarmIdentification = new AlarmIdentificationProperty();
        this.AlarmIdentification.decode(buf);
        int len = buf.readByte();
        if (len > 0) {
            this.AlarmOrEvents = new ArrayList<>();
            for (int i = 0; i < len; i++) {

                AlarmOrEventProperty eventProperty = new AlarmOrEventProperty();
                eventProperty.decode(buf);
                this.AlarmOrEvents.add(eventProperty);
            }


        }


        return this;
    }

}
